using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Utils.UGUI.Effect
{
    [AddComponentMenu("Utils/UGUI/Effect/UVFlip")]
    public class UVFlip : BaseMeshEffect
    {
        [SerializeField] bool horizontal = false;
        [SerializeField] bool vertical = false;
        
        public override void ModifyMesh(VertexHelper vh)
        {
            if (!IsActive() || (!horizontal && !vertical))
                return;

            List<UIVertex> verts = new List<UIVertex>();
            vh.GetUIVertexStream(verts);

            for (int i = 0; i < verts.Count; i++)
            {
                UIVertex v = verts[i];
                v.uv0 = new Vector2(horizontal ? 1 - v.uv0.x : v.uv0.x, vertical ? 1 - v.uv0.y : v.uv0.y);
                verts[i] = v;
            }
            vh.Clear();
            vh.AddUIVertexTriangleStream(verts);
        }
    }
}

